﻿#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <QString>
#include "basicData.h"
#include <QFile>

using namespace std;

enum TYPE {
	PRE_LINE,
	POST_LINE
};


class DataProcessor {
private:
	double defaltWidth_ = 1;		    //默认线宽
	//数据标记
	std::vector<string> pointSymbol;	//点
	std::vector<string> wiresSymbol;	//走线
	std::vector<string> fixLiSymbol;	//边界
	std::vector<string> obss_Symbol;	//多边形障碍物
	std::vector<string> lockLSymbol;    //锁定线段
	std::vector<string> miterSymbol;	//miter
	std::vector<string> diDirSymbol;	//是否差分线
	std::vector<string> diGapSymbol;	//差分线每段线段的gap
	std::vector<string> spcssSymbol;    //差分线每段线段的space

	void setSymbols() {
		pointSymbol = { "#points" ,"#Pts" ,"#pts" };
		wiresSymbol = { "#vec_PolyLine" ,"#lines","#outputLine","#lines_done","#wire2" };		//走线
		fixLiSymbol = { "#fix" };													            //边界线段
		obss_Symbol = { "#obs" ,"#Obs" };								                        //多边形障碍物
		lockLSymbol = { "#lockLines","#lockLine","#lock_lines","#lock_line" };                  //锁定线段
		miterSymbol = { "#LeastSegmentLength" ,"#miter" ,"#Miter" };			                //miter
		diDirSymbol = { "#diffInfo_dir" ,"#DiffInfo_dir","#diffdir","#Diffdir" };		        //是否差分线
		diGapSymbol = { "#diffgap", "#diff_gap", "#diffGap" };			                        //差分线每段线段的gap
		spcssSymbol = { "#spaces", "#Spaces" };                                                 //差分线每段线段的space
	}

	bool isSymbol(string& line, std::vector<string>& symbolArray) {
		bool result = false;
		for (auto&& symbol : symbolArray) {
			result = line.find(symbol) == 0;
			if (result)
				return true;
		}
		return false;
	}

public:
	BasicData* m_data;					//数据文件指针
	QString m_qFilePath = "data";					//文件路径，不含文件名
	QString m_qFileName = "example.txt";			//文件名带后缀，仅名称
	QString m_qFullFileName = "data/example.txt";	//文件全路径，含文件名

public:
	DataProcessor() {
		setSymbols();
	}
	DataProcessor(BasicData* data) {
		setSymbols();
		setData(data);
	}
	~DataProcessor() {};
	void setData(BasicData* data) {
		m_data = data;
	}

	//1.文件解析
	bool readFile(const int type);
	bool readFile(string fullPath, const int type);
	bool readFile(QString qFullPath, const int type);
private:
	struct numbers { //用于读取数据
		double num1;
		double num2;
		double num3;
		double num4;
		double num5;
	};
	bool read_post();
	bool read_txt_to_map();
	bool read_image_to_map();
	bool read_map_routing_res();


	void setMinMax(const numbers& nums);
	void fillBlankData(vector<vector<numbers>>& wireCoordinates,
		vector<vector<vector<numbers>>>& obsCoordinates,
		vector<vector<numbers>>& fixlinesCoordinates,
		vector<vector<numbers>>& locklinesCoordinates,
		vector<vector<double>>& gapCoordinates,
		vector<vector<double>>& spacesCoordinate,
		vector<int>& diffInfo_dirsCoordinates, int add);   //补全缺失的数据,以便能数据一一对应

	//2.数据处理
public:
	//2.0成员变量
	vector<vector<double>> preLineLengths_;							//记录后处理之前每根线段的长度
	vector<vector<double>> postLineLengths_;						//记录后处理之后每根线段的长度
	//2.1数据处理函数
	static vector<LineUI> getRandomPts(int width, int height, int randPtSum = 0);
	static void setPtsFromLines(vector<LineUI>& liness, vector<QPointF>& pts);		//设置线段上所有整数点
	static void setPtsFromLines(vector<vector<LineUI>>& liness, vector<vector<QPointF>>& pts);
	void setLengths(vector<vector<LineUI>>& liness, TYPE type);
	void data_clear(bool readdata_clear = true);

	
	//3.文件输出
	static bool write_PPDT_case();
	static bool write_PPDT_Pre(const QString& qFullName, const vector<LineUI>& pinPairs, const vector<vector<QPointF>>& pathPts, const vector<QString>& mapSymbols);
	static bool write_PPDT_Res(const QString& qFullName, const vector<vector<QPointF>>& pathPts, const vector<QString>& mapSymbols);
	static bool writeOutputData(const QString& qFullName, vector<vector<LineUI>> liness);		//后处理调试数据输出
private:
	bool x_horizontal = false;
	void set_xy_swap();

};


